package com.princess.demok.mvvm.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.princess.demok.base.viewmodel.BaseViewModel
import com.princess.demok.mvvm.repository.HistoryListRepository
import com.princess.demok.net.data.HistoryData
import com.princess.demok.network.initiateRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author: xha
 * @date: 2021/6/7 15:25
 * @Description:
 */
class HistoryListViewModel : BaseViewModel<HistoryListRepository>() {
    val historyData: MutableLiveData<List<HistoryData>> = MutableLiveData()

    fun getData() {
        initiateRequest({ historyData.value = mRepository.getData() }, loadState)
    }

    fun getData1() {
        viewModelScope.launch(Dispatchers.IO) {
            historyData.value = mRepository.getData1()
        }
    }

    fun getData2() {
        viewModelScope.launch {
            historyData.value = withContext(Dispatchers.IO) {
                mRepository.getData1()
            }
        }

    }
}